﻿using System;
using System.Collections.Generic;
using System.Text;
using MediaData;
using System.IO;
using System.Linq;
namespace MediaData
{
    class Program
    {
        static void Main(string[] args)
        {
            MediaRootManager rootManager = new MediaRootManager("tvshows", "anime", "movies");
            rootManager.Refresh();
            foreach (var root in rootManager.Roots)
            {
                Console.WriteLine(root.Volume + " - " + root.Path);
            }
            MovieScanner movieScanner = new MovieScanner("movies");
            TVScanner tvScanner = new TVScanner("tvshows");
            AnimeScanner animeScanner = new AnimeScanner("anime");

            MediaDataSet data = new MediaDataSet();

            MediaDataImporter importer = new MediaDataImporter(rootManager, data);
            importer.Scanners.Add(movieScanner);
            importer.Scanners.Add(animeScanner);
            importer.Scanners.Add(tvScanner);

            importer.BeginCommit += new EventHandler(importer_BeginCommit);
            importer.BeginImport += new EventHandler<MediaImporter.ImportEventArgs>(importer_BeginImport);
            importer.EndImport += new EventHandler<MediaImporter.ImportEventArgs>(importer_EndImport);
            importer.EndCommit += new EventHandler(importer_EndCommit);

            importer.ScanVolumes(rootManager.Roots);


            Console.ReadLine();
            foreach (var title in importer.Data.Titles)
            {
                Console.WriteLine(title.Name);
                foreach (var episode in title.GetEpisodesRows())
                {
                    Console.WriteLine("\t" + episode.Season + "x" + episode.Episode.ToString().PadLeft(2, '0'));
                    foreach (var file in episode.GetEpisodeFilesRows())
                    {
                        Console.WriteLine("\t\t" + file.FilesRowParent.Volume + Path.DirectorySeparatorChar + file.FilesRowParent.Path);
                    }
                }
                foreach (var file in title.GetFilesRows().Where(x => x.GetEpisodeFilesRows().Count() == 0))
                {
                    Console.WriteLine("\t" + file.Volume + Path.DirectorySeparatorChar + file.Path);
                }
            }
            importer.ScanVolumes(rootManager.Roots);
            Console.ReadLine();
        }

        static void importer_EndCommit(object sender, EventArgs e)
        {
            Console.WriteLine("EndCommit");

            Console.WriteLine("-----");
            Console.WriteLine("Done!");
            
        }

        static void importer_EndImport(object sender, MediaImporter.ImportEventArgs e)
        {
            Console.WriteLine("End Import: " + e.Root.Volume + string.Format(" added {0} removed {1}", e.Added, e.Removed));

        }

        static void importer_BeginImport(object sender, MediaImporter.ImportEventArgs e)
        {
            Console.WriteLine("Begin Import: " + e.Root.Volume);
        }

        static void importer_BeginCommit(object sender, EventArgs e)
        {
            Console.WriteLine("BeginCommit");
        }




    }
}
